home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / ghostscript / 8.64 / lib / pdf2dsc.ps < prev    next >
Encoding:
Text File  |  2009-04-17  |  8.2 KB  |  255 lines

  1. %    Copyright (C) 1994, 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: pdf2dsc.ps 8546 2008-02-25 05:48:45Z alexcher $
  14. % pdf2dsc.ps
  15. % read pdf file and produce DSC "index" file.
  16. %
  17. % Input  file is named PDFname
  18. % Output file is named DSCname
  19. %
  20. % Run using:
  21. %  gs -dNODISPLAY -sPDFname=pdffilename -sDSCname=tempfilename pdf2dsc.ps
  22. % Then display the PDF file with
  23. %  gs tempfilename
  24. %
  25. % Modified by Jason McCarty, bug 688071
  26. %       Add PageLabels support.
  27. % Modified by Geoff Keating <geoffk@ozemail.com.au> 21/12/98:
  28. %    Add DocumentMedia, PageMedia comments
  29. %    Use inherited BoundingBox and Orientation
  30. %    Reformat, add new macro 'puts', generally clean up
  31. % Modified by Johannes Plass <plass@dipmza.physik.uni-mainz.de> 1996-11-05:
  32. %    Adds BoundingBox and Orientation if available.
  33. % Modified by rjl/lpd 9/19/96
  34. %    Updates for compatibility with modified pdf_*.ps code for handling
  35. %      page ranges (i.e., partial files) better.
  36. % Modified by Geoff Keating <Geoff.Keating@anu.edu.au> 7/3/96:
  37. %    include Title and CreationDate DSC comments (these are displayed by
  38. %      Ghostview);
  39. %    reduce the size of typical output files by a factor of about 3.
  40. % Modified by L. Peter Deutsch 3/18/96:
  41. %    Removes unnecessary and error-prone code duplicated from pdf_main.ps
  42. % Modified by L. Peter Deutsch for GS 3.33
  43. % Originally by Russell Lang  1995-04-26
  44.  
  45. /PDFfile PDFname (r) file def
  46. /DSCfile DSCname (w) file def
  47. systemdict /.setsafe known { .setsafe } if
  48.  
  49. /puts { DSCfile exch writestring } bind def
  50. /DSCstring 255 string def
  51. /MediaTypes 10 dict def
  52.  
  53.    GS_PDF_ProcSet begin
  54.    pdfdict begin
  55.    PDFfile
  56.    pdfopen begin
  57.    /FirstPage where { pop } { /FirstPage 1 def } ifelse
  58.    /LastPage where { pop } { /LastPage pdfpagecount def } ifelse
  59.  
  60. % scan through for media sizes, keep them in the dictionary
  61.    FirstPage 1 LastPage {
  62.       pdfgetpage /MediaBox pget pop   % MediaBox is a required attribute
  63.       aload pop 
  64.       3 -1 roll sub 3 1 roll exch sub exch
  65.       2 array astore 
  66.       aload 3 1 roll 10 string cvs exch 10 string cvs
  67.       (x) 3 -1 roll concatstrings concatstrings cvn
  68.       MediaTypes 3 1 roll exch put
  69.    } for
  70.  
  71. % write header and prolog
  72.    (%!PS-Adobe-3.0\n) puts
  73.    Trailer /Info knownoget
  74.     {
  75.       dup /Title knownoget
  76.        {
  77.          (%%Title: ) puts
  78.          DSCfile exch write==
  79.        }
  80.       if
  81.       /CreationDate knownoget
  82.        {
  83.          (%%CreationDate: ) puts
  84.          DSCfile exch write==
  85.        }
  86.       if
  87.     }
  88.    if
  89.    % This is really supposed to be sorted by frequency of usage...
  90.    (%%DocumentMedia: )
  91.    MediaTypes { 
  92.       exch pop
  93.       1 index puts
  94.       (y) puts dup 1 get DSCstring cvs puts
  95.       (x) puts dup 0 get DSCstring cvs puts 
  96.       ( ) puts dup 0 get DSCstring cvs puts
  97.       ( ) puts 1 get DSCstring cvs puts 
  98.       ( 70 white ()\n) puts
  99.       pop (%%+ )
  100.    } forall
  101.    pop
  102.  
  103.    (%%Pages: ) puts
  104.    LastPage FirstPage sub 1 add DSCstring cvs puts
  105.    (\n%%EndComments\n) puts
  106.    (%%BeginProlog\n) puts
  107.    (/Page null def\n/Page# 0 def\n/PDFSave null def\n) puts
  108.    (/DSCPageCount 0 def\n) puts
  109.    (/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n) puts
  110.    (GS_PDF_ProcSet begin\npdfdict begin\n) puts
  111.    (%%EndProlog\n) puts
  112.    (%%BeginSetup\n) puts
  113.    DSCfile PDFname write==only
  114.    ( \(r\) file { DELAYSAFER { .setsafe } if } stopped pop\n) puts
  115.    ( pdfopen begin\n) puts
  116.    ( copy_trailer_attrs\n) puts
  117.    (%%EndSetup\n) puts
  118.  
  119.    /.hasPageLabels false def % see "Page Labels" in the PDF Reference
  120.    Trailer /Root knownoget {
  121.      /PageLabels knownoget {
  122.        /PageLabels exch def
  123.        /.pageCounter 1 def
  124.        /.pageCounterType /D def
  125.        /.pagePrefix () def
  126.  
  127.        % (TEXT)  .ToLower  (text)  -- convert text to lowercase -- only letters!
  128.        /.ToLower {
  129.          dup length 1 sub  -1 0 {
  130.            1 index exch 2 copy get 2#00100000 or put
  131.          } for
  132.        } def
  133.  
  134.        % int  .CvAlpha  (int in alphabetic base 26)  -- convert a positive
  135.        % integer to base 26 in capital letters, with 1=A; i.e. A..Z, AA..AZ, ...
  136.        /.CvAlpha { % using cvrs seems futile since this isn't zero-based ...
  137.          [ exch % construct an array of ASCII values, in reverse
  138.          { % the remainder stays on the top of stack
  139.            dup 0 eq { pop exit } if % quit if the value is zero
  140.            dup 26 mod dup 0 eq { 26 add } if % so that the division is correct
  141.            dup 64 add 3 1 roll sub 26 idiv % save the ASCII value and iterate
  142.          } loop ]
  143.          dup length dup string 3 1 roll
  144.          dup -1 1 { % put the letters in a string
  145.            4 copy sub exch 4 -1 roll 1 sub get put
  146.          } for pop pop
  147.        } def
  148.  
  149.        % int  .CvRoman  (int in capital Roman numerals)
  150.        % convert a positive integer to capital Roman numerals
  151.        % return a decimal string if >= 4000
  152.        /.CvRoman {
  153.          dup DSCstring cvs % start with the decimal representation
  154.          exch 4000 lt { % convert only if Roman numerals can represent this
  155.            dup length
  156.            [ [ () (I) (II) (III) (IV) (V) (VI) (VII) (VIII) (IX) ]
  157.              [ () (X) (XX) (XXX) (XL) (L) (LX) (LXX) (LXXX) (XC) ]
  158.              [ () (C) (CC) (CCC) (CD) (D) (DC) (DCC) (DCCC) (CM) ]
  159.              [ () (M) (MM) (MMM) ] ] % Roman equivalents
  160.            () % append the Roman equivalent of each decimal digit to this string
  161.            2 index  -1 1 {
  162.              2 index 1 index 1 sub get
  163.              5 index 5 index 4 -1 roll sub get
  164.              48 sub get concatstrings
  165.            } for
  166.            4 1 roll pop pop pop
  167.          } if
  168.        } def
  169.  
  170.        /PageToString <<
  171.          /D { DSCstring cvs }
  172.          /R { .CvRoman }
  173.          /r { .CvRoman .ToLower }
  174.          /A { .CvAlpha }
  175.          /a { .CvAlpha .ToLower }
  176.        >> def
  177.        /.hasPageLabels true def
  178.      } if
  179.    } if
  180.  
  181.    % process each page
  182.    FirstPage 1 LastPage {
  183.        (%%Page: ) puts
  184.  
  185.        .hasPageLabels {
  186.          dup 1 sub PageLabels exch numoget dup null ne {
  187.            % page labels changed at this page, reset the values
  188.            dup /S known { dup /S get } { null } ifelse
  189.            /.pageCounterType exch def
  190.  
  191.            dup /P known { dup /P get } { () } ifelse
  192.            /.pagePrefix exch def
  193.  
  194.            dup /St known { /St get } { pop 1 } ifelse
  195.            /.pageCounter exch def
  196.          } { pop } ifelse
  197.  
  198.          % output the page label
  199.          (\() .pagePrefix
  200.          .pageCounterType //null ne dup {
  201.            PageToString .pageCounterType known and
  202.          } if { % format the page number
  203.            .pageCounter dup 0 gt { % don't try to format nonpositive numbers
  204.              PageToString .pageCounterType get exec
  205.            } {
  206.              DSCstring cvs
  207.           } ifelse
  208.          } { () } ifelse
  209.          (\)) concatstrings concatstrings concatstrings puts
  210.  
  211.          /.pageCounter .pageCounter 1 add def
  212.        } {
  213.          dup DSCstring cvs puts
  214.        } ifelse
  215.        ( ) puts
  216.        dup DSCstring cvs puts
  217.        (\n) puts
  218.  
  219.        dup pdfgetpage
  220.        dup /MediaBox pget pop
  221.          (%%PageMedia: y) puts
  222.      aload pop 3 -1 roll sub DSCstring cvs puts
  223.      (x) puts exch sub DSCstring cvs puts
  224.      (\n) puts
  225.        dup /CropBox pget {
  226.          (%%PageBoundingBox: ) puts
  227.          {DSCfile exch write=only ( ) puts} forall
  228.          (\n) puts
  229.        } if
  230.        /Rotate pget {
  231.          (%%PageOrientation: ) puts
  232.          90 div cvi 4 mod dup 0 lt {4 add} if
  233.          [(Portrait) (Landscape) (UpsideDown) (Seascape)] exch get puts
  234.          (\n) puts
  235.        } if
  236.  
  237.        DSCfile exch DSCstring cvs writestring
  238.        ( DoPDFPage\n) puts
  239.     } for
  240.    currentdict pdfclose
  241.    end
  242.    end
  243.    end
  244. % write trailer
  245. (%%Trailer\n) puts
  246. (currentdict pdfclose\nend\nend\nend\n) puts
  247. (%%EOF\n) puts
  248. % close output file and exit
  249. DSCfile closefile
  250. quit
  251. % end of pdf2dsc.ps
  252.